//
//  ZCStatusOriginView.swift
//  weibo
//
//  Created by Ning Xie on 16/8/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class ZCStatusOriginView: UIView {

    var originViewConstraint:Constraint?
    var statusViewModel: ZCStatusViewModel? {
        didSet {
            //  设置控件的数据
            if let profile = statusViewModel?.status?.user?.profile_image_url {
            //绑定头像数据
                headImageView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //绑定昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //设置微博数据
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.origianlAttributedString
            
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
            
            //配图
            originViewConstraint?.uninstall()
            if let url = statusViewModel?.status?.pic_urls where url.count > 0 {
                picImageView.hidden = false
                videoView.hidden = true
                picImageView.picUrls = url
                
                self.snp_updateConstraints(closure: { (make) in
                    
                   self.originViewConstraint = make.bottom.equalTo(picImageView).offset(StatusTableViewCellMargin).constraint
                })
                
            }else{
                picImageView.hidden = true
                
                //视频
                if let url = statusViewModel?.status?.video_urls where false {
                    videoView.hidden = false
                    videoView.videoURL = url
                    
                    self.snp_updateConstraints(closure: { (make) in
                        
                        self.originViewConstraint = make.bottom.equalTo(videoView).offset(StatusTableViewCellMargin).constraint
                    })
                }else{
                    
                    videoView.hidden = true
                    self.snp_updateConstraints(closure: { (make) in
                        
                        self.originViewConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                    })
                }
            }
            
            
            //会员认证
            verifiedTypeImageView.image = statusViewModel?.verifiedImage
            //会员等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            //发微博时间
            timeLabel.text = statusViewModel!.timeAgo
            
        }
    }
    
    //  头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 17
        imageView.clipsToBounds = true
        return imageView
    
    }()
    //  认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.orangeColor())
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGrayColor())
    //  微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        
        //  多行显示
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
        
    }()
    
    //微博图片
    private lazy var picImageView:ZCStatusPicView = {
        let view = ZCStatusPicView()
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
    //微博视频
    private lazy var videoView:ZCAVPlayerView = {
        let view = ZCAVPlayerView()
        view.backgroundColor = UIColor.blackColor()
        
        
        return view
    }()
   

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //  添加控件
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picImageView)
        addSubview(videoView) //微博视频
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing).offset(-5)
            make.centerY.equalTo(headImageView.snp_bottom).offset(-5)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        picImageView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        videoView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.trailing.equalTo(contentLabel)
            make.height.equalTo(100)
        }
        
        //  关键约束  原创微博视图的底部约束 = 微博内容的底部约束 + 间距
        
        self.snp_makeConstraints { (make) -> Void in
            self.originViewConstraint = make.bottom.equalTo(videoView).offset(StatusTableViewCellMargin).constraint
        }
        
        
    }

    
    
}
